<template>
  <div class="role-management">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>角色管理</span>
          <el-button type="primary" @click="handleAdd">
            <el-icon><Plus /></el-icon>
            新增角色
          </el-button>
        </div>
      </template>

      <!-- 角色列表 -->
      <el-table
        :data="roleList"
        stripe
        border
        style="width: 100%"
      >
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="roleName" label="角色名称" width="150" />
        <el-table-column prop="roleCode" label="角色编码" width="150" />
        <el-table-column prop="description" label="描述" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="row.status === 1 ? 'success' : 'danger'">
              {{ row.status === 1 ? '启用' : '禁用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="180" />
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="{ row }">
            <el-button type="primary" size="small" @click="handleEdit(row)">
              编辑
            </el-button>
            <el-button type="danger" size="small" @click="handleDelete(row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 角色表单对话框 -->
    <RoleForm
      :visible="formVisible"
      :role-data="currentRole"
      :is-edit="isEdit"
      @update:visible="formVisible = $event"
      @success="handleFormSuccess"
    />
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'
import type { Role } from '@/api/user'
import RoleForm from './components/RoleForm.vue'

// 角色列表
const roleList = ref<Role[]>([
  {
    id: 1,
    roleName: '超级管理员',
    roleCode: 'SUPER_ADMIN',
    description: '系统超级管理员，拥有所有权限',
    status: 1,
    createTime: '2024-01-01 00:00:00'
  },
  {
    id: 2,
    roleName: '普通管理员',
    roleCode: 'ADMIN',
    description: '普通管理员，拥有部分管理权限',
    status: 1,
    createTime: '2024-01-01 00:00:00'
  },
  {
    id: 3,
    roleName: '普通用户',
    roleCode: 'USER',
    description: '普通用户，只有基本查看权限',
    status: 1,
    createTime: '2024-01-01 00:00:00'
  }
])

// 表单对话框
const formVisible = ref(false)
const currentRole = ref<Role | null>(null)
const isEdit = ref(false)

// 新增角色
const handleAdd = () => {
  currentRole.value = null
  isEdit.value = false
  formVisible.value = true
}

// 编辑角色
const handleEdit = (role: Role) => {
  currentRole.value = { ...role }
  isEdit.value = true
  formVisible.value = true
}

// 删除角色
const handleDelete = async (role: Role) => {
  try {
    await ElMessageBox.confirm(
      `确定要删除角色 "${role.roleName}" 吗？`,
      '确认删除',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }
    )
    
    // 模拟删除操作
    const index = roleList.value.findIndex(item => item.id === role.id)
    if (index > -1) {
      roleList.value.splice(index, 1)
      ElMessage.success('删除成功')
    }
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('删除失败')
    }
  }
}

// 表单成功回调
const handleFormSuccess = (role: Role) => {
  formVisible.value = false
  
  if (isEdit.value) {
    // 更新角色
    const index = roleList.value.findIndex(item => item.id === role.id)
    if (index > -1) {
      roleList.value[index] = { ...role }
    }
  } else {
    // 新增角色
    role.id = Date.now() // 模拟ID生成
    role.createTime = new Date().toLocaleString()
    roleList.value.push({ ...role })
  }
}

// 初始化
onMounted(() => {
  // 这里可以调用API获取角色列表
})
</script>

<style scoped lang="scss">
.role-management {
  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
}
</style>
